package com.custom.topic;

import com.custom.CustomBindingMqConfiguration;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class TopicProducer {
    @Autowired
    private AmqpTemplate template;

    public void send(){
        String[] keys = {"quick.orange.rabbit",
                "lazy.orange.elephant",
                "quick.orange.fox",
                "lazy.brown.fox",
                "lazy.pink.rabbit",
                "quick.brown.fox",
                "orange",
                "quick.orange.male.rabbit",
                "lazy.orange.male.rabbit",
                "*.orange.*"};
        for (int i = 1; i <= 5; i++) {
            String routingKey = getRoutingKey(keys);
            String message = "123" + " seq：" + i;
            System.out.println(" [" + i + " ] Sent '" + routingKey + "':'" + message + "'");
            template.convertAndSend(TopicMqConfiguration.EXCHANGE_NAME, routingKey,message);
        }
    }

    private  String getRoutingKey(String[] keys) {
        Random random = new Random();
        int i = random.nextInt(keys.length);
        return keys[i];
    }

}
